package org.orqlog.handler;

import org.orqlog.encoder.RpcRequest;
import org.orqlog.service.UserServiceImpl;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.lang.reflect.Method;

public class UserServerHandler extends ChannelInboundHandlerAdapter {


    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

        // 通过反射调用
        Class userService = Class.forName(((RpcRequest) msg).getClassName());
        Method method = userService.getMethod(((RpcRequest) msg).getMethodName());
        Object userServiceObj = userService.newInstance();
        method.invoke(userServiceObj);

//        System.out.println("The method to call: " + ((RpcRequest) msg).getMethodName());

        ctx.writeAndFlush("success");
    }
}
